import gradio as gr
import numpy as np
import tensorflow as tf
from PIL import Image, ImageDraw

def recognize_digit(image):
    model_path = 'D:\HONORShare\JisuanjiShijue\PcVision\optimal'
    loaded_model = tf.keras.models.load_model(model_path)
    image = Image.fromarray(image)  # 将数组转换为图像对象
    image = image.resize((28, 28)).convert('L')  # 调整图像大小并转换为灰度图像
    image_array = np.array(image)  # 将图像转换为numpy数组
    flattened_image = image_array.reshape(1, 28, 28, 1) / 255.0  # 将图像数组展平为一维数组并进行归一化
    prediction = loaded_model.predict(flattened_image)
    return np.argmax(prediction)

iface = gr.Interface(fn=recognize_digit, inputs='sketchpad', outputs="number")
iface.launch()

